package com.appbee.cm.client.startup;

import javax.inject.Inject;
import javax.inject.Provider;

import com.appbee.cm.client.event.EditModeChange;
import com.appbee.cm.client.nav.presenter.NavControl;
import com.appbee.cm.client.nav.presenter.NavItemWrap;
import com.appbee.cm.client.nav.ui.AdminPanel;
import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.layout.LayoutManager;
import com.appbee.core.client.layout.PageLayout;
import com.appbee.core.client.place.INamePlace;
import com.appbee.core.client.shell.IShell;
import com.appbee.core.client.startup.StartupChain;
import com.appbee.core.client.startup.StartupLink;
import com.appbee.user.client.AccessControl;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.google.gwt.user.client.ui.HasWidgets.ForIsWidget;

public class AdminPanelLink implements StartupLink
{
	private final AccessControl accessControl;
	private final Provider<AdminPanel> adminPanelProvider;
	private final EventBus eventBus;
	private final IShell shell;
	private final PageStyle pageStyle;
	private final NavControl navControl;
	private final LayoutManager layoutManager;

	@Inject
	public AdminPanelLink(	AccessControl accessControl,
							Provider<AdminPanel> adminPanelProvider,
							EventBus eventBus,
							IShell shell,
							PageStyle pageStyle,
							NavControl navControl,
							LayoutManager layoutManager)
	{
		this.accessControl = accessControl;
		this.adminPanelProvider = adminPanelProvider;
		this.eventBus = eventBus;
		this.shell = shell;
		this.pageStyle = pageStyle;
		this.navControl = navControl;
		this.layoutManager = layoutManager;
	}

	@Override
	public void start(ForIsWidget container, StartupChain chain)
	{
		chain.start(container);
		if (accessControl.isMemberOrAdmin())
		{
			container.add(adminPanelProvider.get());
			// Edit mode
			EditModeChange.registerForProxyType(eventBus,
												new EditModeChange.Handler()
												{

													@Override
													public void onChange(EditModeChange event)
													{
														if (event.isEditMode())
															shell.addStyleName(pageStyle
																	.editMode());
														else
															shell.removeStyleName(pageStyle
																	.editMode());
													}
												});
		}

		// Set template
		eventBus.addHandler(PlaceChangeEvent.TYPE,
							new PlaceChangeEvent.Handler()
							{

								@Override
								public void onPlaceChange(PlaceChangeEvent event)
								{
									Place place = event.getNewPlace();
									if (!(place instanceof INamePlace))
										return;

									// Find navigation item
									INamePlace namePlace = (INamePlace) place;
									NavItemWrap navItem = navControl
											.getByPlace(namePlace);

									if (navItem == null)
										return;

									PageLayout layout = layoutManager
											.getById(navItem.getWrappedItem()
													.getLayoutId());
									if (layout == null)
										return;
									// Set layoutstyle
									shell.setMainPanelStyleName(layout
											.styleName());

								}
							});
	}

}
